package com.xaicode.auth;

import com.xaicode.auth.security.DataPermissionInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * spring容器启动完成后调用
 * <p>
 * 该方法是在容器启动完成之后才执行，所以这里可以从spring容器中拿到其他已经注入的bean
 */
@Slf4j
@Component
@Order(1) /* 此 order 非必须，含多个 runner 时可考虑增加顺序 */
public class SelfApplicationRunner implements ApplicationRunner {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        // 加载自定义数据权限拦截器
        DataPermissionInterceptor dataPermissionInterceptor = new DataPermissionInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(dataPermissionInterceptor);
        }
        log.info("注入 DataPermissionInterceptor");

    }
}
